package com.inifitness.tenant.core.config.jackson;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;

import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * TODO:{描述}
 *
 * @author sunjinfeng
 * @since 2022/12/28 15:37
 */

@Configuration
public class JacksonConfig {

  private final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(
      "yyyy-MM-dd HH:mm:ss");
  private final DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

  @Bean
  public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
    Map<Class<?>, JsonSerializer<?>> serializers = new HashMap<>(8);
    serializers.put(LocalDate.class, new LocalDateSerializer(dateFormatter));
    serializers.put(LocalDateTime.class, new LocalDateTimeSerializer(dateTimeFormatter));
    serializers.put(Long.TYPE, ToStringSerializer.instance);
    serializers.put(Long.class, ToStringSerializer.instance);
    serializers.put(BigInteger.class, ToStringSerializer.instance);
    serializers.put(BigDecimal.class, ToStringSerializer.instance);
    return builder -> builder.serializersByType(serializers);
  }
}
